package com.jumi.microservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jumi.microservice.common.utils.ObjectUtils;
import com.jumi.microservice.constant.ExceptionEnum;
import com.jumi.microservice.constant.StatusEnum;
import com.jumi.microservice.domain.Log;
import com.jumi.microservice.domain.LogDetailVO;
import com.jumi.microservice.domain.OperateLogQuery;
import com.jumi.microservice.domain.Priority;
import com.jumi.microservice.domain.vo.LogVO;
import com.jumi.microservice.mapper.LogMapper;
import com.jumi.microservice.mapper.PriorityMapper;
import com.jumi.microservice.service.LogService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 日志表 服务实现类
 * </p>
 *
 * @author liuz
 * @since 2020-07-18
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class LogServiceImpl extends ServiceImpl<LogMapper, Log> implements LogService {

    /**
     * 日志管理mapper组件
     */
    @Autowired
    private LogMapper logMapper;

    @Autowired
    private PriorityMapper priorityMapper;

    /**
     * 分页查询操作日志
     *
     * @param operateLogQuery 查询条件
     * @return 操作日志集合
     */
    @Override
    public Page<Log> listOperateLogs(OperateLogQuery operateLogQuery) {
        ExceptionEnum.FAIL_NULL.doThrowIf(operateLogQuery == null);
        LambdaQueryWrapper<Log> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Log::getIsDel, StatusEnum.STATUS_NO.getCode());
        wrapper.like(StringUtils.isNotEmpty(operateLogQuery.getDepartmentName()), Log::getDepartmentName, operateLogQuery.getDepartmentName());
        wrapper.like(StringUtils.isNotEmpty(operateLogQuery.getUserName()), Log::getUserName, operateLogQuery.getUserName());
        wrapper.like(StringUtils.isNotEmpty(operateLogQuery.getMobile()), Log::getMobile, operateLogQuery.getMobile());
        wrapper.like(StringUtils.isNotEmpty(operateLogQuery.getPriorityId()), Log::getPriorityId, operateLogQuery.getPriorityId());
        wrapper.like(StringUtils.isNotEmpty(operateLogQuery.getPriorityName()), Log::getPriorityName, operateLogQuery.getPriorityName());
        wrapper.like(StringUtils.isNotEmpty(operateLogQuery.getAction()), Log::getOperateAction, operateLogQuery.getAction());
        wrapper.ge(StringUtils.isNotEmpty(operateLogQuery.getOperateTimeFrom()), Log::getOperateTime, operateLogQuery.getOperateTimeFrom());
        wrapper.le(StringUtils.isNotEmpty(operateLogQuery.getOperateTimeTo()), Log::getOperateTime, operateLogQuery.getOperateTimeTo());
        wrapper.eq(operateLogQuery.getOperateType()!=null, Log::getOperateType, operateLogQuery.getOperateType());
        wrapper.orderByDesc(Log::getId, Log::getOperateTime);
        //根据按钮标识获取操作菜单
        if(StringUtils.isNotEmpty(operateLogQuery.getPriorityCode())){
            Priority p = priorityMapper.selectOne(
                    Wrappers.<Priority>lambdaQuery()
                            .eq(Priority::getPriorityCode,operateLogQuery.getPriorityCode())
                            .eq(Priority::getIsDel, StatusEnum.STATUS_NO.getCode())
                            .eq(Priority::getStatus,StatusEnum.STATUS_YES.getCode()));
            if(p!=null){
                //获取父级菜单目录
                String pname = priorityMapper.selectParentMenuById(p.getId());
                //拼接父菜单信息
                if(StringUtils.isNotEmpty(pname)&&pname.contains(",")){
                    pname = pname.replace(",","->");
                    wrapper.like( Log::getPriorityName, pname);
                }
            }
        }
        Page<Log> logs = logMapper.selectPage(new Page<>(operateLogQuery.getPageNum(), operateLogQuery.getPageSize()), wrapper);
        return logs;
    }

    /**
     * 一键回收/恢复操作
     * @param vo 搜索条件
     * @param type 操作类型(0:一键回收,1:一键恢复)
     */
    @Override
    public void recyclingOrRecovering(LogVO vo, Integer type) {
        LambdaQueryWrapper<Log> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(vo.getDepartmentId() != null, Log::getDepartmentId, vo.getDepartmentId());
        wrapper.like(org.apache.commons.lang3.StringUtils.isNotEmpty(vo.getUserName()), Log::getUserName, vo.getUserName());
        wrapper.like(org.apache.commons.lang3.StringUtils.isNotEmpty(vo.getMobile()), Log::getMobile, vo.getMobile());
        wrapper.like(org.apache.commons.lang3.StringUtils.isNotEmpty(vo.getPriorityName()), Log::getPriorityName, vo.getPriorityName());
        wrapper.eq(vo.getPriorityId() != null, Log::getPriorityId, vo.getPriorityId());
        wrapper.eq(vo.getOperateType() != null, Log::getOperateType, vo.getOperateType());
        wrapper.like(org.apache.commons.lang3.StringUtils.isNotEmpty(vo.getOperateAction()), Log::getOperateAction, vo.getOperateAction());
        wrapper.ge(org.apache.commons.lang3.StringUtils.isNotEmpty(vo.getStartTime()), Log::getOperateTime, vo.getStartTime());
        wrapper.le(org.apache.commons.lang3.StringUtils.isNotEmpty(vo.getEndTime()), Log::getOperateTime, vo.getEndTime());
        wrapper.orderByDesc(Log::getOperateTime);
        List<Log> logs = logMapper.selectList(wrapper);
        logs.stream().forEach(log->{
            log.setIsDel(type);
            baseMapper.updateById(log);
        });

    }

    /**
     * 将操作日志DO对象集合转换为VO对象集合
     *
     * @param logs 账号登录日志DO 集合
     * @return List<LogDetailVO> 操作日志VO对象集合
     */
    private List<LogDetailVO> convertOperateLogDoList2VoList(List<Log> logs) {
        List<LogDetailVO> logVoList = null;
        try {
            logVoList = ObjectUtils.convertList(logs, LogDetailVO.class);
        } catch (Exception e) {
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return logVoList;
    }

}
